#include "pthread.h"
#include "stdio.h"
#include "malloc.h"
#include "Locker.h"
typedef struct _PrivInfo{    
pthread_mutex_t mutex;
}PrivInfo;


static enum Ret  locker_pthread_lock(Locker* thiz){
    PrivInfo* priv = (PrivInfo*)thiz->priv;
    int ret = pthread_mutex_lock(&priv->mutex);
    return ret == 0 ? RET_OK : RET_FAIL;
}


static enum Ret  locker_pthread_unlock(Locker* thiz){
    PrivInfo* priv = (PrivInfo*)thiz->priv;
    int ret = pthread_mutex_unlock(&priv->mutex);
    return ret == 0 ? RET_OK : RET_FAIL;
}

static enum Ret  locker_pthread_destroy(Locker* thiz){
    PrivInfo* priv = (PrivInfo*)thiz->priv;
    int ret = locker_pthread_destroy(&priv->mutex);
    return ret == 0 ? RET_OK : RET_FAIL;
}


Locker* locker_pthread_create(void){    
	Locker* thiz = (Locker*)malloc(sizeof(Locker) + sizeof(PrivInfo));    
	if(thiz != NULL){        
		PrivInfo* priv = (PrivInfo*)thiz->priv;
	        thiz->lock    = locker_pthread_lock;
	        thiz->unlock  = locker_pthread_unlock;
	        thiz->destroy = locker_pthread_destroy;
	        pthread_mutex_init(&(priv->mutex), NULL);
        }
        return thiz;
}


//single thread
//DList* dlist = dlist_create(NULL, NULL, NULL);
//multi thread
//DList* dlist = dlist_create(NULL, NULL, locker_pthread_create());
